﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.Base;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.FormBean.PhanQuyen;
using DTTB.DTO;
using DTTB.MV2.QUANLYNHANSUTIENLUONG.Models.LinQ.PhanQuyen;
using DTTB.SERVICE;
using DTTB.Domain;

namespace DTTB.MV2.QUANLYNHANSUTIENLUONG.BussinessDelegate.PhanQuyen
{
    public class DepartmentsServiceDelegate : BaseServiceDelegate<DepartmentsDto, Departments>
    {
        PhanQuyenDataContext PhanQuyenDB = new PhanQuyenDataContext();
        DepartmentsService DepartmentsService = new DepartmentsService();
        

        public DepartmentsDto GetDepartmentsById(string id)
        {
            return DepartmentsService.GetDepartmentsById(id);
        }

        public DepartmentsDto BuildNewDepartments(DepartmentsForm form)
        {
            DepartmentsDto newDepartment = new DepartmentsDto();
            newDepartment.DepartmentName = form.DepartmentName;
            newDepartment.DepartmentDesc = form.DepartmentDesc;
            newDepartment.IdChief = form.IdChief;
            newDepartment.CreateDate = DateTime.Now;
            newDepartment.UpdateDate = DateTime.Now;
            newDepartment.CreateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            newDepartment.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            newDepartment.IsDelete = false;

            return newDepartment;
        }

        public DepartmentsDto BuildEditDepartments(DepartmentsForm form)
        {
            DepartmentsDto newDepartment = this.DepartmentsService.GetDepartmentsById(form.DepartmentID);
            newDepartment.DepartmentName = form.DepartmentName;
            newDepartment.DepartmentDesc = form.DepartmentDesc;
            newDepartment.IdChief = form.IdChief;
            //newDepartment.CreateDate = DateTime.Now;
            newDepartment.UpdateDate = DateTime.Now;
            //newDepartment.CreateBy = SessionDetail.USERID;
            newDepartment.UpdateBy = Convert.ToInt32(HttpContext.Current.Session["USERID"]);
            //newDepartment.IsDelete = false;

            return newDepartment;
        }

        public bool AddNewDepartments(DepartmentsDto departmentsDto)
        {
            return this.DepartmentsService.AddNewDepartments(departmentsDto);
        }

        public bool AddNewDepartments(DepartmentsDto departmentsDto, ref int idReturn)
        {
            return this.DepartmentsService.AddNewDepartments(departmentsDto, ref idReturn);
        }

        public bool Update(DepartmentsDto departmentsDto)
        {
            return this.DepartmentsService.Update(departmentsDto);
        }

        public bool ShiftDelete(string departmentsId)
        {
            return this.DepartmentsService.ShiftDelete(departmentsId);
        }

        public bool Delete(string departmentsId)
        {
            return this.DepartmentsService.Delete(departmentsId);
        }

        public DepartmentsDto GetDepartmentsByName(string departmentName)
        {
            return this.DepartmentsService.GetDepartmentsByName(departmentName);
        }


        #region Phần code chuuyển sang LinQ
        
        /// <summary>
        /// Lấy tất cả phòng ban không bị đánh dấu delete
        /// </summary>
        /// <returns>Danh sách phòng ban</returns>
        public IList<TB_Department> GetAllDepartments()
        {
            return PhanQuyenDB.TB_Departments.Where(p => p.IsDelete == false).OrderBy(p => p.DepartmentName).ToList();
        }

        public SelectList BuildDepartmentSelectList()
        {
            IList<TB_Department> list;
            
            list = GetAllDepartments();//test

            IList<object> selectList = new List<object> { new { value = 0, Text = "[-- Chọn phòng ban --]" } };

            foreach (var dto in list)
            {
                selectList.Add(new { value = dto.ID, Text = dto.DepartmentName });
            }

            return new SelectList(selectList, "Value", "Text");
        }

        public void BuildSelectListForDepartmentsForm(DepartmentsForm form)
        {
            form.TieuChiSearchDropDownList = BuildSelectListSearchDepartments();
        }

        /// <summary>
        /// Build selectList - Tiêu chí tìm kiếm phòng ban
        /// </summary>
        /// <returns>SelectList</returns>
        public SelectList BuildSelectListSearchDepartments()
        {
            IList<object> tieuChiSearchList = new List<object>();
            tieuChiSearchList.Add(new { value = 1, Text = "Phòng ban" });
            //tieuChiSearchList.Add(new{value = 1, Text = "..."});

            return new SelectList(tieuChiSearchList, "Value", "Text");
        }

        /// <summary>
        /// Tìm kiếm phòng ban theo một tiêu chí
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns>Danh sách các phòng ban</returns>
        public IList<TB_Department> SearchDepartments(int key, string value)
        {
            IList<TB_Department> departmentList = new List<TB_Department>();
            var departments = from department in PhanQuyenDB.TB_Departments
                              where department.IsDelete == false
                            select department;
            if (key == 1) // Tìm phòng ban theo Tên
                departmentList = departments.Where(p => p.DepartmentName.Contains(value)).OrderBy(p => p.DepartmentName).ToList();

            return departmentList;
        }


        #endregion
    }
}
